home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / SkinnedMesh / SkinnedMesh.frm < prev    next >
Text File  |  2001-10-08  |  9KB  |  303 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSkinnedMesh 
  3.    Caption         =   "Skinned Mesh"
  4.    ClientHeight    =   6015
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   7530
  8.    Icon            =   "SkinnedMesh.frx":0000
  9.    LinkTopic       =   "Form3"
  10.    ScaleHeight     =   401
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   502
  13.    StartUpPosition =   3  'Windows Default
  14. End
  15. Attribute VB_Name = "frmSkinnedMesh"
  16. Attribute VB_GlobalNameSpace = False
  17. Attribute VB_Creatable = False
  18. Attribute VB_PredeclaredId = True
  19. Attribute VB_Exposed = False
  20.  
  21. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  22. '
  23. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  24. '
  25. '  File:       skinnedMesh.frm
  26. '  Content:    Animate Skinned Geometry
  27. '
  28. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  29.  
  30. Option Explicit
  31.  
  32. Dim Character As CD3DFrame
  33. Dim Animation As CD3DAnimation
  34. Dim MediaDir As String
  35. Dim m_bInit As Boolean
  36. Dim m_bMinimized As Boolean
  37.  
  38.  
  39. Private Sub Form_Load()
  40.     Dim hr As Long
  41.     
  42.     Me.Show
  43.     DoEvents
  44.     
  45.     
  46.     'find a path to our media
  47.     MediaDir = FindMediaDir("tiny.x")
  48.     D3DUtil_SetMediaPath MediaDir
  49.     
  50.     ' Initialize D3D
  51.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  52.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  53.     ' If all fail it will display a message box indicating so.
  54.     '
  55.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  56.     If Not (m_bInit) Then End
  57.                 
  58.     
  59.     ' Create new D3D mesh and animation objects
  60.     InitDeviceObjects
  61.     
  62.     ' Sets the state for those objects and the current D3D device
  63.     RestoreDeviceObjects
  64.     
  65.     ' Start our timer
  66.     DXUtil_Timer TIMER_start
  67.     
  68.     ' Run the simulation forever
  69.     ' See Form_Keydown for exit processing
  70.     Do While True
  71.     
  72.         ' Increment the simulation
  73.         FrameMove
  74.         
  75.         ' Render one image of the simulation
  76.         If Render Then
  77.             
  78.             ' Present the image to the screen
  79.             D3DUtil_PresentAll g_focushwnd
  80.         End If
  81.         
  82.         ' Allow for events to get processed
  83.         DoEvents
  84.         
  85.     Loop
  86.                    
  87.     
  88. End Sub
  89.  
  90. '-----------------------------------------------------------------------------
  91. ' Name: FrameMove()
  92. ' Desc:
  93. '-----------------------------------------------------------------------------
  94. Sub FrameMove()
  95.     Dim apptime As Single
  96.     
  97.     'get ellapsed time since start of application
  98.     apptime = DXUtil_Timer(TIMER_GETAPPTIME)
  99.     
  100.     'Have our animation pose our character
  101.     Animation.SetTime (apptime) * 4000
  102.             
  103.     'Rotate the character
  104.     Character.AddRotation COMBINE_replace, 0, 0, 1, 3.14 + (apptime) / 8
  105.     
  106.     'Update all frame matrices (required for skinning)
  107.     Character.UpdateFrames
  108.  
  109. End Sub
  110.  
  111.  
  112. '-----------------------------------------------------------------------------
  113. ' Name: Render()
  114. ' Desc:
  115. '-----------------------------------------------------------------------------
  116. Function Render() As Boolean
  117.     Dim hr As Long
  118.    
  119.     Render = False
  120.     'See what state the device is in.
  121.     hr = g_dev.TestCooperativeLevel
  122.     If hr = D3DERR_DEVICENOTRESET Then
  123.         g_dev.Reset g_d3dpp
  124.         RestoreDeviceObjects
  125.     End If
  126.     
  127.     'dont bother rendering if we are not ready yet
  128.     If hr <> 0 Then Exit Function
  129.     Render = True
  130.  
  131.     'Clear the background to ARGB grey
  132.     D3DUtil_ClearAll &HFF9090FF
  133.  
  134.  
  135.     'Start the Scene
  136.     g_dev.BeginScene
  137.     
  138.     'Render the character
  139.     Character.RenderSkins
  140.     
  141.     'End the scene
  142.     g_dev.EndScene
  143.     
  144.  
  145. End Function
  146.  
  147. '-----------------------------------------------------------------------------
  148. ' Name: InitDeviceObjects()
  149. ' Desc:
  150. '-----------------------------------------------------------------------------
  151. Sub InitDeviceObjects()
  152.     
  153.     'Create an Animation object to hold any animations
  154.     Set Animation = New CD3DAnimation
  155.     
  156.     'Load a skinned character
  157.     Set Character = D3DUtil_LoadFromFileAsSkin(MediaDir + "tiny.x", Nothing, Animation)
  158.     
  159. End Sub
  160.  
  161.  
  162. '-----------------------------------------------------------------------------
  163. ' Name: RestoreDeviceObjects()
  164. ' Desc:
  165. '-----------------------------------------------------------------------------
  166. Sub RestoreDeviceObjects()
  167.     
  168.     'Set up some lights and camera
  169.     g_lWindowWidth = Me.ScaleWidth
  170.     g_lWindowHeight = Me.ScaleHeight
  171.     D3DUtil_SetupDefaultScene
  172.     
  173.     'position the camera
  174.     D3DUtil_SetupCamera vec3(0, 800, 200), vec3(0, 0, 200), vec3(0, 0, 1)
  175.     
  176. End Sub
  177.  
  178. '-----------------------------------------------------------------------------
  179. ' Name: InvalidateDeviceObjects()
  180. ' Desc: Place code to release non managed objects here
  181. '-----------------------------------------------------------------------------
  182. Sub InvalidateDeviceObjects()
  183.     'all objects are managed
  184. End Sub
  185.  
  186. '-----------------------------------------------------------------------------
  187. ' Name: DeleteDeviceObjects()
  188. ' Desc:
  189. '-----------------------------------------------------------------------------
  190. Sub DeleteDeviceObjects()
  191.     Set Animation = Nothing
  192.     Set Character = Nothing
  193.     m_bInit = False
  194. End Sub
  195.  
  196. '-----------------------------------------------------------------------------
  197. ' Name: Form_KeyDown()
  198. ' Desc: Process key messages for exit and change device
  199. '-----------------------------------------------------------------------------
  200. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  201.      Select Case KeyCode
  202.         
  203.         Case vbKeyEscape
  204.             Unload Me
  205.             
  206.         Case vbKeyF2
  207.                 
  208.             ' Pause the timer
  209.             DXUtil_Timer TIMER_STOP
  210.             
  211.             ' Bring up the device selection dialog
  212.             ' we pass in the form so the selection process
  213.             ' can make calls into InitDeviceObjects
  214.             ' and RestoreDeviceObjects
  215.             frmSelectDevice.SelectDevice Me
  216.             
  217.             ' Restart the timer
  218.             DXUtil_Timer TIMER_start
  219.             
  220.         Case vbKeyReturn
  221.         
  222.             ' Check for Alt-Enter if not pressed exit
  223.             If Shift <> 4 Then Exit Sub
  224.             
  225.             ' If we are windowed go fullscreen
  226.             ' If we are fullscreen returned to windowed
  227.             If g_d3dpp.Windowed Then
  228.                  D3DUtil_ResetFullscreen
  229.             Else
  230.                  D3DUtil_ResetWindowed
  231.             End If
  232.                              
  233.             ' Call Restore after ever mode change
  234.             ' because calling reset looses state that needs to
  235.             ' be reinitialized
  236.             RestoreDeviceObjects
  237.            
  238.     End Select
  239. End Sub
  240.  
  241.  
  242. '-----------------------------------------------------------------------------
  243. ' Name: Form_Resize()
  244. ' Desc: hadle resizing of the D3D backbuffer
  245. '-----------------------------------------------------------------------------
  246. Private Sub Form_Resize()
  247.  
  248.     ' If D3D is not initialized then exit
  249.     If Not m_bInit Then Exit Sub
  250.     
  251.     ' If we are in a minimized state stop the timer and exit
  252.     If Me.WindowState = vbMinimized Then
  253.         DXUtil_Timer TIMER_STOP
  254.         m_bMinimized = True
  255.         Exit Sub
  256.         
  257.     ' If we just went from a minimized state to maximized
  258.     ' restart the timer
  259.     Else
  260.         If m_bMinimized = True Then
  261.             DXUtil_Timer TIMER_start
  262.             m_bMinimized = False
  263.         End If
  264.     End If
  265.     
  266.     ' Dont let the window get too small
  267.     If Me.ScaleWidth < 10 Then
  268.         Me.width = Screen.TwipsPerPixelX * 10
  269.         Exit Sub
  270.     End If
  271.     
  272.     If Me.ScaleHeight < 10 Then
  273.         Me.height = Screen.TwipsPerPixelY * 10
  274.         Exit Sub
  275.     End If
  276.  
  277.     'reset and resize our D3D backbuffer to the size of the window
  278.     D3DUtil_ResizeWindowed Me.hwnd
  279.     
  280.     'All state get losts after a reset so we need to reinitialze it here
  281.     RestoreDeviceObjects
  282.     
  283. End Sub
  284.  
  285. '-----------------------------------------------------------------------------
  286. ' Name: Form_Unload()
  287. ' Desc:
  288. '-----------------------------------------------------------------------------
  289. Private Sub Form_Unload(Cancel As Integer)
  290.     DeleteDeviceObjects
  291.     End
  292. End Sub
  293.  
  294.  
  295. '-----------------------------------------------------------------------------
  296. ' Name: Form_Unload()
  297. ' Desc:
  298. '-----------------------------------------------------------------------------
  299. Public Function VerifyDevice(flags As Long, format As CONST_D3DFORMAT) As Boolean
  300.     If flags = D3DCREATE_HARDWARE_VERTEXPROCESSING Then Exit Function
  301.     VerifyDevice = True
  302. End Function
  303.